/**
* All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
*
* MmsProvinceMarketingInfoService
* 省区行销配置信息表新
*
* 修改纪录
* 2021-07-08 版本：1.0 lvzhijun 创建。
* @version 版本：1.0
* @author 作者：lvzhijun
* 创建日期：2021-07-08
*/
package com.topscomm.mms.service.impl;

import com.lowagie.text.Paragraph;
import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.cbo.pojo.CboItemDetailsEntity;
import com.topscomm.cbo.pojo.CboItemsEntity;
import com.topscomm.cbo.service.impl.CboItemDetailsService;
import com.topscomm.cbo.service.impl.CboItemsService;
import com.topscomm.mms.pojo.MmsDeptExtraInfoEntity;
import com.topscomm.mms.pojo.MmsProvinceMarketingInfoEntity;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.tap.common.RedisDatacodeStore;
import com.topscomm.tap.common.SessionUserBean;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.mms.service.IMmsProvinceMarketingInfoService;
import com.topscomm.mms.service.impl.auto.MmsProvinceMarketingInfoServiceAuto;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(value = "mmsProvinceMarketingInfoService")
@Scope(value = "singleton")
public class MmsProvinceMarketingInfoService extends MmsProvinceMarketingInfoServiceAuto implements IMmsProvinceMarketingInfoService {
	@Autowired
	public CboItemsService cboItemsService;
	@Autowired
	public CboItemDetailsService cboItemDetailsService;
	@Autowired
	private RedisDatacodeStore redisDatacodeStore;
	//Marketingpost code in CBOITEMS
	 public static final String MarketingPostItemCode = "MmsMarketingPost";
	 //Frontend form paraMap key
	 public static final String NewMarketingPostParam = "newMarketingpost";

    /**
    	 * @description: 插入新的行销岗位
    	 * @param:
    	 * @throws:
    	 * @author: xinfushuo
    	 * @date: 2021/7/29
    	 * @modify:
	     * @return 新行销岗位的entity
    	 */
    @Transactional(rollbackFor = Exception.class )
    public CboItemDetailsEntity addProvincePost(Map<String, Object> paraMap){
		//获取mmsMarketingpost item 对象
		CboItemsEntity marketingPostItemEntity = cboItemsService.queryObjectByWhere(CboItemsEntity.FieldCode + " = " + "'"+MarketingPostItemCode+"'");
		//查询现存所有行销岗位
		List<CboItemDetailsEntity> items = cboItemDetailsService.queryByWhere(
				CboItemDetailsEntity.FieldItemid + "=" + marketingPostItemEntity.getId()
				+ " AND " + CboItemDetailsEntity.FieldEnabled + " = 1 "
				+ "ORDER BY " + " CAST( " + CboItemDetailsEntity.FieldCode + " AS INT )  DESC");
		//创建新行销岗位并插入
		CboItemDetailsEntity newMarketingPost = new CboItemDetailsEntity();

		if (paraMap.containsKey(NewMarketingPostParam)) {
			newMarketingPost.setOrgid(getSessionUserBean().getOrg().getId());
			newMarketingPost.setEnabled(1);
			//数据库里没有数据则设code为1,否则Max+1
			if(items.isEmpty()){
				newMarketingPost.setCode(ConvertUtil.convertToString(1));
			}else{
				int code = ConvertUtil.convertToInt(items.get(0).getCode())+1;
				newMarketingPost.setCode(ConvertUtil.convertToString(code));
			}
			newMarketingPost.setName(paraMap.get(NewMarketingPostParam).toString());
			newMarketingPost.setItemid(marketingPostItemEntity.getId());
		}

		newMarketingPost=cboItemDetailsService.insert(newMarketingPost);

		//redis直接在原value尾部添加
		List<String> itemField = new ArrayList<>();
		itemField.add(CboItemDetailsEntity.FieldId);
		itemField.add(CboItemDetailsEntity.FieldName);
		itemField.add(CboItemDetailsEntity.FieldCode);
		redisDatacodeStore.appendData(MarketingPostItemCode,newMarketingPost.convertToMap(itemField));
		return newMarketingPost;
	}

	@Override
	protected void updateAfter(Map<String, Object> map) throws ServiceException {
		super.updateAfter(map);
		this.addValueToRedis(MmsProvinceMarketingInfoEntity.tableName);
	}

	@Override
	protected void afterInsert(BasicEntity entity) throws ServiceException {
		super.afterInsert(entity);
		this.addValueToRedis(MmsProvinceMarketingInfoEntity.tableName);
	}

	@Override
	protected void deleteAfter(String whereSql) {
		super.deleteAfter(whereSql);
		this.addValueToRedis(MmsProvinceMarketingInfoEntity.tableName);
	}
}
